package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.Passwd;
import com.example.result.Result;
import com.example.service.PasswdService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 班级控制层
 * @date 2019-12-31
 */
@RestController
@RequestMapping("/api")
public class PasswdController {
    @Autowired
    PasswdService passwdService;

    // ===============================密码管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加密码信息
     */
    @RequestMapping(value = "/passwds", method = RequestMethod.POST)
    public Result addPasswd(@RequestBody String json) {
        // 将JSON数据转换为Course对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Passwd passwd = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Passwd.class);

        return passwdService.addPasswd(passwd);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新密码信息
     */
    @RequestMapping(value = "/passwds", method = RequestMethod.PUT)
    public Result updatePasswd(@RequestBody String json) {
        // 将JSON数据转换为Course对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Passwd passwd = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Passwd.class);
        return passwdService.updatePasswd(passwd);

    }

    /**
     * @param passwdId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据密码编号删除密码信息
     */
    @RequestMapping(value = "/passwds/{passwdId}", method = RequestMethod.DELETE)
    public Result deletePasswdById(@PathVariable("passwdId") String passwdId) {
        return passwdService.deletePasswdById(passwdId);

    }

    /**
     * @return 以JSON格式返回所有密码信息
     * @describe 查询所有密码信息
     */
    @RequestMapping(value = "/Passwd", method = RequestMethod.GET)
    public Result queryAllPasswd() {
        return passwdService.queryAllPasswd();

    }

    /**
     * @return 以JSON格式返回所有密码信息和用户信息
     * @describe 查询所有密码信息和用户信息
     */
    @RequestMapping(value = "/passwds/all", method = RequestMethod.GET)
    public Result queryAllPasswdAndUser() {

        return passwdService.queryAllPasswdAndUser();
    }

    /**
     * @param passwdId
     * @return 以JSON格式返回该密码信息
     * @describe 根据编号查询密码信息
     */
    @RequestMapping(value = "/Passwd/{passwdId}", method = RequestMethod.GET)
    public Result queryPasswdById(@PathVariable("passwdId") String passwdId) {
        return passwdService.queryPasswdById(passwdId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加密码信息
     */
    @RequestMapping(value = "/passwds/excels", method = RequestMethod.POST)
    public Result addPasswdByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<Passwd> list = JSON.parseArray(parseObject.getString("json"), Passwd.class);

        System.out.println(list);

        return passwdService.addPasswdByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据密码编号批量删除密码信息
     */
    @RequestMapping(value = "/passwds/dels", method = RequestMethod.POST)
    public Result deletePasswdByIds(@RequestBody Map<String, String> Ids) {

        return passwdService.deletePasswdByIds(Ids);
    }

    /**
     * @param Ids
     * @return 重置成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据密码编号批量重置密码信息
     */
    @RequestMapping(value = "/passwds/resets", method = RequestMethod.POST)
    public Result resetPasswdByIds(@RequestBody String json) {

        System.out.println("重置密码" + json);
        return passwdService.resetPasswdByIds(json);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 密码信息模糊查询
     */
    @RequestMapping(value = "/passwds/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return passwdService.serachByInfo(info);

    }
}
